<?php 
class Category extends Module
{
	function Category($row)
	{
		Module::Module($row);
		
		if(User::have_permit(ADMIN_CATEGORY)){
			$cmd=Url::get('cmd');
			$mod=Url::get('mod');
			
			switch($mod){
				case 'zone':
					switch($cmd){
						case 'delete':
//							AZLib::del_cache_zone_cat();
							$id=(int)Url::get('id',0);
							if($id){
								$cat_zone=DB::select('category_zone','id='.$id);
								
								if($cat_zone){
									DB::delete('category_zone',"id = {$cat_zone['id']}");
									DB::delete('category_zone_cat',"zoneid = {$cat_zone['id']}");
								}
								AZLib::getCats(0,1);
							}
							Url::redirect_current(array("mod"));
							break;
						case 'add':
						case 'edit':
							require_once 'forms/ZoneEdit.php';
							$this->add_form(new ZoneEditForm());
							break;
						/*case 'move_zonecat_up':
						case 'move_zonecat_down':
								$id=(int)Url::get('id',0);
								if($id){
									$zonecat=DB::select('category_zone_cat','catid='.(int)Url::get('catid',0).' AND zoneid='.$id);
									if($zonecat){
										if($cmd=='move_zonecat_up'){
											$nearest_zonecat=DB::select('category_zone_cat','position<'.$zonecat['position'].' AND zoneid='.$id.' ORDER BY position DESC');
										}
										else{
											$nearest_zonecat=DB::select('category_zone_cat','position>'.$zonecat['position'].' AND zoneid='.$id.' ORDER BY position ASC');
										}
										if($nearest_zonecat){
											// Vị trí của category trong zone đã được chuẩn hóa
											DB::update('category_zone_cat',array('position'=>$nearest_zonecat['position']),'id='.$zonecat['id']);
											DB::update('category_zone_cat',array('position'=>$zonecat['position']),'id='.$nearest_zonecat['id']);	
										}
										else
										{
											//Vị trí không chuẩn, vị trí tiếp theo bằng đúng vị trí của category hiện tại
											$nearest_zonecat=DB::select('category_zone_cat','position='.$zonecat['position'].' AND catid<>'.$zonecat['catid'].' AND zoneid='.$id.' ORDER BY position ASC');
											if($nearest_zonecat){
												if($cmd=='move_zonecat_up')
												{
													$zonecat['position']++;
												}
												elseif($zonecat['position'] > 0)
												{
													$zonecat['position']--;
												}
												DB::update('category_zone_cat',array('position'=>$zonecat['position']),'id='.$nearest_zonecat['id']);
											}
										}
									}
								}
								Url::redirect_current(array('mod'=>'zone','cmd'=>'edit','id'=>$id));
								break;*/
						case 'move_up':
						case 'move_down':
//								AZLib::del_cache_zone_cat();
								$id=(int)Url::get('id',0);
								if($id){
									$cat_zone=DB::select('category_zone','id='.$id);
									if($cat_zone){
										if($cmd=='move_up'){
											$nearest_cat_zone=DB::select('category_zone','position<'.$cat_zone['position'].' ORDER BY position DESC');
										}
										else{
											$nearest_cat_zone=DB::select('category_zone','position>'.$cat_zone['position'].' ORDER BY position ASC');
										}
										if($nearest_cat_zone){
											DB::update('category_zone',array('position'=>$nearest_cat_zone['position']),'id='.$cat_zone['id']);					
											DB::update('category_zone',array('position'=>$cat_zone['position']),'id='.$nearest_cat_zone['id']);	
										}
										AZLib::getCats(0,1);
									}
								}
								Url::redirect_current(array("mod"));
								break;
						case 'show':
						case 'hide':
							//AZLib::del_cache_zone_cat();
							$id=(int)Url::get('id',0);
							if($id){
								$cat_zone=DB::select('category_zone','id='.$id);
								if($cat_zone){
									if($cmd=='hide')
										$status = 0;
									else 
										$status = 1;
										
									DB::update('category_zone',array('status'=>$status),"id=$id");
									AZLib::getCats(0,1);
								}
							}
							Url::redirect_current(array("mod"));
							break;
						default:
						require_once 'forms/Zone.php';
						$this->add_form(new ZoneForm());
					
					}
					break;
				default:
				{ 
					switch($cmd){				
						case 'del_image':
							$id=(int)Url::get('id',0);
							//Xoá file ảnh
							if($id){
								$cat = DB::select('category',"id=$id");
								if($cat){
									AZLib::ftp_image_delete_file('category/'.$id.'.gif',$cat['img_server']);
									//@fopen('http://'.IMAGE_PATH.'?del_cat=1',"r");//Xoá cache file ảnh
								}
								Url::redirect_current();
							}
							break;
						case 'delete':
							$selected_ids=Url::get('selected_ids');
							
							if($selected_ids){
								$ids=implode(',',$selected_ids);
								if($ids!=''){
//									AZLib::del_cache_zone_cat();
							
									DB::delete('category','id IN('.$ids.')');
								}
								AZLib::getCats(0,1);
							}
							Url::redirect_current();
							break;
						case 'del_cache':
//							AZLib::del_cache_zone_cat();
							AZLib::getCats(0,1);
							Url::redirect_current();
							break;
							
						case 'add':
						case 'edit':
							require_once 'forms/CategoryEdit.php';
							$this->add_form(new EditCategoryForm());
							break;
						case 'move_up':
						case 'move_down':
								$id=(int)Url::get('id',0);
								if($id){
									$cat=DB::select('category','id='.$id);
									if($cat){
//										AZLib::del_cache_zone_cat();
							
										if($cmd=='move_up'){
											$nearest_cat=DB::select('category','position<'.$cat['position'].' AND parent_id='.$cat['parent_id'].' ORDER BY position DESC');
										}
										else{
											$nearest_cat=DB::select('category','position>'.$cat['position'].' AND parent_id='.$cat['parent_id'].' ORDER BY position ASC');
										}
										if($nearest_cat){
											DB::update('category',array('position'=>$nearest_cat['position']),'id='.$cat['id']);					
											DB::update('category',array('position'=>$cat['position']),'id='.$nearest_cat['id']);	
										}
										AZLib::getCats(0,1);
									}
								}
								Url::redirect_current();
								break;
						case 'set_home':
						case 'show':
						case 'hide':
							$id=(int)Url::get('id',0);
							if($id){
								$cat=DB::select('category','id='.$id);
								if($cat){
//									AZLib::del_cache_zone_cat();
							
									if($cmd=='set_home')
										$status='HOME';
									elseif($cmd=='hide')
										$status='HIDE';
									else 
										$status='SHOW';
										
									DB::update('category',array('status'=>$status),"id=$id");
									AZLib::getCats(0,1);
								}
							}
							Url::redirect_current();
							break;
						case 'validated':
						case 'un_validated':
							$id=(int)Url::get('id',0);
							if($id){
								$cat=DB::select('category','id='.$id);
								if($cat){
									if($cmd=='validated')
										$must_validated = 1;
									elseif($cmd=='hide')
										$must_validated = 0;
									
									if($cat['parent_id']){	
										DB::update('category',array('must_validated'=>$must_validated),"id=$id");
									}
									else{
										DB::update('category',array('must_validated'=>$must_validated),"parent_id=$id OR id=$id");
									}
									AZLib::getCats(0,1);
								}
							}
							Url::redirect_current();
							break;
						case 'phone_verified':
						case 'un_phone_verified':
							$id=(int)Url::get('id',0);
							if($id){
								$cat=DB::select('category','id='.$id);
								if($cat){
									if($cmd=='phone_verified')
										$must_phone_verified = 1;
									elseif($cmd=='hide')
										$must_phone_verified = 0;
									
									if($cat['parent_id']){		
										DB::update('category',array('must_phone_verified'=>$must_phone_verified),"id=$id");
									}
									else{
										DB::update('category',array('must_phone_verified'=>$must_phone_verified),"parent_id=$id OR id=$id");
									}
									AZLib::getCats(0,1);
								}
							}
							Url::redirect_current();
							break;
						case 'optimize':
							$this->optimize();
							AZLib::getCats(0,1);
							Url::redirect_current();
							break;
						case 'filter':
							require_once 'forms/Filter.php';
							$this->add_form(new FilterForm());
							break;
						case 'filter_list':
							require_once 'forms/FilterList.php';
							$this->add_form(new FilterListForm());
							break;
						case '':
							require_once 'forms/Category.php';
							$this->add_form(new ListCategoryForm());
							break;
							
						default: 
							Url::redirect_current();
							break;
					}
				}
				break;
			}
		}
		else{
			Url::access_denied();
		}
	}
	
	function optimize(){
		$sql="SELECT id,name,position,parent_id FROM category ORDER BY position";
		
		$re=DB::query($sql);
		$items		=array();
		$sub_items	=array();
		
		
		if($re){
			$last=array();
			$op_pos=1;
			while ($row=mysql_fetch_assoc($re)) {
				if($row['parent_id']){
					$sub_items[$row['parent_id']][$row['id']]=$row;
				}
				else{//Top cat
					$row['op_pos']=$op_pos;
					//$row['sub_cat']=array();
					$op_pos++;
					$items[$row['id']]=$row;
				}
			}
		}
		
		foreach ($sub_items as $id=>$sub_item){
			$op_pos=1;
			foreach ($sub_item as $sub_id=>$sub){
				$sub['op_pos']=$op_pos;
				$op_pos++;
				$items[$sub_id]=$sub;
			}
		}
		
		$sql_arr=array();
		
		foreach ($items as $id=>$item){
			if($item['position']!=$item['op_pos']){
				$change_pos=$item['op_pos']-$item['position'];
				$sql_arr[$change_pos][]=$id;
			}
		}
		
		foreach ($sql_arr as $change_pos=>$id_items){
			$ids=implode(',',$id_items);
			$sql="UPDATE category SET position=position".($change_pos>0?'+'.$change_pos:'-'.(0-$change_pos))." WHERE id IN(".$ids.")";
			DB::query($sql);		
		}
	}
	
	#Remove filter_group khỏi danh mục
	static function remove_cat_group_filer($catid,$fgid)
	{
		AZLib::getFilters();
		
		if($catid && $fgid && isset(CGlobal::$allCategories[$catid],CGlobal::$all_gfilters[$fgid]))
		{
			$filter_group	= DB::select('filter_group','id='.$fgid);
			
			if($filter_group && $filter_group['catids'])
			{
				$arr = explode(',',$filter_group['catids']);
				
				if(in_array($catid,$arr))
				{
					$arr = array_unique(array_flip($arr));
					
					unset($arr[$catid]);
					
					$catids = '';
					
					if($arr)
					{
						$catids = implode(',', array_flip($arr));
					}
					
					DB::update("filter_group",array("catids"=>$catids),"id=$fgid");
				}
			}
			
			AZLib::getFilters(1);
		}
	}
}
?>